import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

void main() {
  test("ifKey", () {
    Json map = {
      "name": "233",
      "age": 23,
      "null": null,
    };

    // 检测null值
    bool detectedNull = false;
    map.ifKey<bool>(
      "null",
      // 如果没找到，则判断为检测到
      onKey: (_) => throw Exception("不应该调用此函数"),
      onNull: () => detectedNull = true,
    );
    expect(detectedNull, isTrue);

    // 检测字符串
    String? str;
    map.ifKey<String>(
      "name",
      onKey: (value) => str = value,
    );
    expect(str, "233");

    // 检测int
    int? i;
    map.ifKey<int>(
      "age",
      onKey: (value) => i = value,
    );
    expect(i, 23);

    // 错误的格式
    expect(() => map.ifKey<String>("age", onKey: (_) {}), isBug);
  });
}
